#include <iostream>

#include "Character.h"
#include "Random.h"

using namespace std;



/**
 * Tells if a Character is dead or not.
 * @param void
 * @return true if Health Point are 0 or below. Else false is returned.
 */
bool Character::isDead()
{
	if (m_iHP <= 0)
	{
		return true;
	}
	return false;
}


/**
 * During a fight the Character looses health points. This function is used to substract them.
 * @param int damage - damage taken
 * @return void
 */
void Character::takeDamage(int damage)
{
	cout << getName() << " looses " << damage << " health points." << endl;
	setHP(getHP() - damage);
}



/**
 * The fighting routine.
 * attacker's attack is compared with the defense of the target (hit roll of 1 always goes through thr next step)
 * if the target is hit the dice are rolled to check if the armor is pierced
 * if it is the case, the target looses health points given by the weapon
 * @see getPhysicalAttack()
 * @see getPhysicalDefense()
 * @see getName()
 * @see getArmorFactor()
 * @see Item::getName()
 * @see Item::getDamage()
 * @see Item::getBypassRoll()
 * @param Character& target - the defender
 * @return void
 */
void Character::attack(Character& target)
{
	int hitRoll = 0;
	int hitValue = 0;
	
	cout << getName() << " attacks the " << target.getName() << endl;
	hitValue = getPhysicalAttack() - target.getPhysicalDefense();
	hitRoll = Random(1,20);
	if ((hitRoll <= hitValue) || (hitRoll == 1))
	{
		cout << target.getName() << " is Hit!" << endl;
		if (m_rightHand != 0)
		{
			if ((m_rightHand->getBypassRoll() > target.getArmorFactor()))
			{
				cout << target.getName() << " takes damage from " << m_rightHand->getName() << endl;
				target.takeDamage(m_rightHand->getDamage());
			}
			else
			{
				cout << m_rightHand->getName() << " could not pierce the armor of " << target.getName() << endl;
			}
		}
		if (m_leftHand != 0)
		{
			if ((m_leftHand->getBypassRoll() > target.getArmorFactor()))
			{
				cout << target.getName() << " takes damage from " << m_leftHand->getName() << endl;
				target.takeDamage(m_leftHand->getDamage());
			}
			else
			{
				cout << m_leftHand->getName() << " could not pierce the armor of " << target.getName() << endl;
			}
		}
	}
	else
	{
		cout << getName() << " missed " << target.getName() << "!" << endl;
	}
}





/**
 * Returns the armor factor the armor worn by the Character
 * @see Item::getArmorFactor()
 * @param void
 * @return int - ArmorFactor
 */
int Character::getArmorFactor()
{
	if (m_body != 0)
		return (m_body->getArmorFactor());
	else
		return 0;
}
